﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;

using SSATool.Common.Logging;
using SSATool.Common.Util.Properties;

#endregion 

namespace SSATool.Common.Util
{

    /// <summary>
    /// Specifies the data connection for the local storage.
    /// </summary>
    public class DisposeBase : IDisposable
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private bool _disposed = false;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns true, if the current context has been disposed.
        /// </summary>
        protected bool IsDisposed
        {
            get { return _disposed; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DisposeBase instance, no public constructor
        /// needed because it's abstract.
        /// </summary>
        protected DisposeBase()
        {
        }

        /// <summary>
        /// Dispose instance manually.
        /// </summary>
        ~DisposeBase()
        {
            Dispose(false);
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Implement dispose interface in order to support runtime support.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            // Use SupressFinalize in case a subclass
            // of this type implements a finalizer.
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes the managed and unmanaged resources.
        /// </summary>
        /// <param name="disposing">True to dispose the managed resources, otherwise false.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                ReleaseUnmanagedResources();

                if (disposing)
                    ReleaseManagedResources();

                _disposed = true; // indicate that the instance has been disposed
                Logger<DisposeBase>.InfoFormat(Resources.LogDispBaseInstanceDisposed, GetType().FullName);
            }
        }

        /// <summary>
        /// Releases unmanaged resources.
        /// </summary>
        protected virtual void ReleaseUnmanagedResources()
        {
        }

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected virtual void ReleaseManagedResources()
        {
        }

        /// <summary>
        /// Throws an exception if the current object has been disposed.
        /// </summary>
        protected void EnsureNotDisposed()
        {
            if (IsDisposed)
                throw new InvalidOperationException(Resources.ExcDispBaseDisposed);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}